const app = getApp();
var {
  Api
} = require('../../api/api')
const network = require("../../api/network.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    domain: Api.domain,
    show: false, //紧缩押题弹框是否出来
    page: 1,
    page_size: 20,
    isBig: 2, //是否购买大题
    items: '',
    ios: app.globalData.ios, //是否为ios
    show3: false, //进群弹框
    show666: false, //进群弹框
    jinqun: '', //大群进群东西
    register: false,
    phone: false,
    clicidata666:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init()
    this.jinqun()
    this.init2()

    this.setData({
      isBig: wx.$cache.get('userinfo').is_big_subject
    })
  },
  xia666(e){
    var books_id = e.currentTarget.dataset.id
    if (network.get('can').is_examine == 1 && app.globalData.ios) {
      wx.showToast({
        title: '请用安卓手机，苹果手机不支持',
        icon: 'none'
      });
    }
    let {
      url
    } = e.currentTarget.dataset
    let {
      name
    } = e.currentTarget.dataset
    if (this.data.isBig == 1) {
      
      this.setData({
        clicidata666:e,
        show666:true
      });

    } else {




      this.setData({
        show: true
      })
      
    }
  },
  clog(e){
    let data=e.detail;
    this.xia(data);
    //console.log(e);
  },
  xia(e) {
    var books_id = e.currentTarget.dataset.id
    if (network.get('can').is_examine == 1 && app.globalData.ios) {
      wx.showToast({
        title: '请用安卓手机，苹果手机不支持',
        icon: 'none'
      });
    }
    let {
      url
    } = e.currentTarget.dataset
    let {
      name
    } = e.currentTarget.dataset
    if (this.data.isBig == 1) {
      this.downxia(url, name)
      wx.$http.get(wx.$get.big_chapter,{
        book_id:books_id
      }).then(res => {
        console.log('big_chapter',res);
      })
    } else {




      this.setData({
        show: true
      })

    }
  },
  downxia(url, name) { //下载文件
    var that = this
    wx.showLoading({
      title: '下载中',
    })
    var fileType = url.substring(url.lastIndexOf(".") + 1)
    console.log(fileType, 'dmdm')
    wx.downloadFile({
      url: url, //仅为示例，并非真实的资源
      filePath: wx.env.USER_DATA_PATH + '/' + name + '.' + fileType,
      success: (res) => {
        console.log(res)
        wx.hideLoading()
        console.log(fileType, url, 'ddddddd')
        if (res.statusCode === 200) {
          console.log(fileType)
          if (fileType == 'png' || fileType == 'jpg' || fileType == 'jpeg') {
            // that.bao(res.tempFilePath)
            wx.previewImage({
              current: url,
              urls: [url]
            })
          } else {
            // const manage = wx.getFileSystemManager();
            // manage.saveFile({
            //   tempFilePath: res.tempFilePath,
            //   filePath:wx.env.USER_DATA_PATH + "/" + name,
            //   success (res) {
            //     const savedFilePath = res.savedFilePath
            //     console.log(res)
            console.log(res.filePath)
            wx.openDocument({
              // filePath: savedFilePath,
              // filePath:wx.env.USER_DATA_PATH + "/" + name,
              filePath: res.filePath,
              fileType: fileType,
              showMenu: true,
              success: function (res) {
                console.log('打开文档成功', res)
              },
              fail(err) {
                console.log(err, '失败')
                wx.showToast({
                  title: '打开失败',
                  icon: 'none',
                  duration: 2000
                });

               

              }
              //   })
              // }
            })
          }
        }
      },
    })
  },
  init() { //书籍列表
    var that = this
    var params = {
      page: 1,
      page_size: this.data.page_size
    }
    network.requestLoading(Api.GET.big_book, params, 3, '', 'GET', '', function (res) { //成功的方法
      console.log('书籍列表', res)
      that.setData({
        items: res
      })
    }, function () {})
  },
  godetail(e) {
    if (network.get('can').is_examine == 1 && app.globalData.ios) {
      wx.showToast({
        title: '请用安卓手机，苹果手机不支持',
        icon: 'none'
      })
      return
    }
    let {
      id
    } = e.currentTarget.dataset
    if (this.data.isBig == 1) {
      wx.navigateTo({
        url: `/pages/chapterList/chapterList?id=${id}`,
      })
    } else {
      this.setData({
        show: true
      })
    }
  },
  jinqun() { //大群进群参数
    var that = this
    var params = {

    }
    network.requestLoading(Api.GET.big_setting, params, 3, '', 'GET', '', function (res) { //成功的方法
      console.log('大群进群参数', res)
      that.setData({
        jinqun: res,
      })
    }, function () {})
  },
  pulldown() { //书籍列表
    this.setData({
      page: this.data.page += 1
    })
    var that = this
    var params = {
      page: this.data.page,
      page_size: this.data.page_size
    }
    network.requestLoading(Api.GET.big_book, params, 3, '', 'GET', '', function (res) { //成功的方法
      console.log('书籍列表', res)
      if (res.list.length != 0) {
        that.setData({
          items: [...that.data.items, ...res.list]
        })
      }
    }, function () {})
  },
  init2() { //我的
    var that = this
    var params = {

    }
    wx.$http.get(wx.$get.member_info,{}).then(res => {
      console.log('我的', res)
      that.setData({
        isBig: res.is_big_subject,
      })
      network.put('userinfo', res)
    })
    // network.requestLoading(Api.GET.my_info, params, 1, '', 'GET', '', function (res) { //成功的方法    
    //   console.log('我的', res)
    //   that.setData({
    //     isBig: res.is_big_subject,
    //   })
    //   network.put('userinfo', res)
    // }, function () {})
  },
  bay() { //获取大题资格
    var that = this
    var params = {
      type: 1
    }
    console.log(app.globalData.ios, 11111112212121212)
    if (network.get('can').is_examine == 1 && app.globalData.ios) {
      wx.showToast({
        title: '请用安卓手机，苹果手机不支持',
        icon: 'none'
      })

    } else {
      network.requestLoading(Api.GET.big_qualifications, params, 1, '', 'GET', '', function (res) { //成功的方法
        console.log('购买大题', res)
        wx.requestPayment({
          timeStamp: res.timeStamp,
          nonceStr: res.nonceStr,
          package: res.package,
          signType: 'MD5',
          paySign: res.paySign,
          success(res) {
            wx.showToast({
              title: '购买成功',
              icon: 'success',
              duration: 2000
            })
            that.init2()
            that.setData({
              show: false,
              show3: true
            })
          },
          fail(res) {}
        })
      }, function () {})
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (network.get('key')) {
      this.setData({
        register: false
      })
      if (network.get('phone')) {
        this.setData({
          phone: false
        })
      } else {
        this.setData({
          phone: true
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.pulldown()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})